package com.fingard.dsp.bank.directbank.citic03;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

public class CITIC03Base extends DirectBase {

    protected void writeCommonHeader(XmlTextWriter xmlWriter, CITIC03Header p_bankHeader) throws Exception{
        if(StringHelper.isNullOrEmpty(p_bankHeader.action))
        {
            throw new Exception("未赋值action");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        xmlWriter.writeStartElement("stream");
        xmlWriter.writeElementString("action", p_bankHeader.action);
        xmlWriter.writeElementString("userName", bankFront.loginName);
    }

    protected String getCharset(){
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String p_reqStr) throws Exception{

        String[] retStr = new String[]{"",""};
        WriteBankLogLn("2.提交银行的地址："+bankFront.serverURL);
        WriteBankLogStep2(p_reqStr);
        //发送银行前置
        WebRequest tmpRequest = new WebRequest(bankFront.serverURL);
        String charsetName = getCharset();
        tmpRequest.setConnectTimeout(10000);
        tmpRequest.setRequestProperty("Connection", "close");
        tmpRequest.setRequestProperty("Content-type", "application/xml");
        int readTimeout = getReadTimeout();
        WriteBankLogLn("readTimeout:" + readTimeout/1000 + "秒");
        tmpRequest.setReadTimeout(readTimeout);
        byte[] tmpWriteBytes = p_reqStr.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }

    protected static boolean isActiveRetCode(String retCode){
        if(retCode != null){
            if ("AAAAAAA".equals(retCode) || "AAAAAAB".equals(retCode) || "AAAAAAC".equals(retCode) || "AAAAAAE".equals(retCode) || "AAAAAAF".equals(retCode) || "BBBBBBB".equals(retCode) || "CCCCCCC".equals(retCode) || "PBRA001".equals(retCode)){
                return true;
            }
        }
        return false;
    }
}
